package com.boot.producer.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.boot.producer.entity.BasicInfo;
import com.boot.producer.service.BasicInfoService;
import com.boot.producer.service.dto.WeeklyInfoDto;
import com.boot.utils.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZhangZhilei
 * @since 2019-01-29
 */
@Controller
@RequestMapping("/basicinfo")
@Api("驾驶基础信息")
public class BasicInfoController {
    @Autowired
    BasicInfoService basicInfoService;

    @ApiOperation("取列表")
    @ResponseBody
    @GetMapping("/list")
    public List<BasicInfo> list(){
        List<BasicInfo> basicInfos = basicInfoService.selectList(new EntityWrapper<BasicInfo>().orderBy("time"));
        return basicInfos;
    }

    @ApiOperation("根据日期获取单个驾驶信息")
    @ResponseBody
    @GetMapping("/getByDate")
    public BasicInfo getByDate(@RequestParam("time")Date time){
        BasicInfo basicInfo = basicInfoService.selectOne(new EntityWrapper<BasicInfo>().eq("time", time));
        return basicInfo;
    }

    @ApiOperation("保存行驶数据")
    @ResponseBody
    @PostMapping("/saveInfo")
    public void saveInfo(@RequestBody BasicInfo basicInfo){
        System.out.println(basicInfo);
        basicInfoService.insert(basicInfo);
    }
    @ApiOperation("完善行驶数据信息")
    @ResponseBody
    @PostMapping("/editInfo")
    public void editInfo(@RequestBody BasicInfo basicInfo) {
        basicInfoService.updateById(basicInfo);
    }
    @ApiOperation("根据时间删除")
    @ResponseBody
    @GetMapping("/deleteByDate")
    public void deleteByDate(@RequestParam("date") Date date) {
        basicInfoService.delete(new EntityWrapper<BasicInfo>().eq("time",date));
    }
    @ApiOperation("根据范围获取基本信息")
    @ResponseBody
    @GetMapping("/getByRange")
    public BasicInfo getByRange(@RequestParam("begin") Date begin,@RequestParam("end") Date end) {
        return basicInfoService.getInfoByDateRange(begin, end);
    }
    @ApiOperation("获取一周基本信息")
    @ResponseBody
    @GetMapping("/selectWeekInfo")
    public BasicInfo selectWeekInfo(){
        return basicInfoService.getInfoByDateRange(DateUtil.backDate(), new Date());
    }
    @ApiOperation("获取统计信息")
    @GetMapping("/analysisInfo")
    @ResponseBody
    public WeeklyInfoDto selectAnalysisInfo(@RequestParam("begin") Date begin,@RequestParam("end") Date end){

        List<BasicInfo> basicInfos = basicInfoService.selectList(new EntityWrapper<BasicInfo>().between("time",begin,end).orderBy("time"));
        return basicInfoService.getWeeklyInfo(basicInfos, DateUtil.differentDays(begin, end),end);
    }
    @ApiOperation("获取一周日期")
    @GetMapping("/listWeekDates")
    @ResponseBody
    public List<Date>  listOneWeekDates(@RequestParam("begin") Date begin,@RequestParam("end") Date end){
        List<Date> dates = new ArrayList<>();
        ;
        for (int i = DateUtil.differentDays(begin,end);i>=0; i--){
            dates.add(DateUtil.changeDay(end,-i));
        }
        System.out.println(dates);
        return dates;
    }

}

